// pages/integral/detail/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        img: [],
        is_product: false,
        isChoose: false, //是否已选规格
        detail: '',
        num: "",
        sku: "",
        store_id: "",
        intro: "",
        ids: "",
        sku_product: "",
        is_user_show: true,
        is_user: 0,
        showUser: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const that = this
        wx.removeStorageSync('items')
        that.getAddressList()
        app.http.post(app.api.IntegralDetail, {
            id: options.id
        }, (res) => {
            let intro = res.data.intro.replace(/<img /g, '<img style="display:block" alt=""')
            that.setData({
                store_id: options.store_id,
                detail: res.data,
                intro,
                
            })

            // WxParse.wxParse('content', 'html',intro, that, 5);
        })
    },
            //   登录
            login() {
                if (wx.getStorageSync('token') == "") {
                    this.setData({
                        is_user_show: false,
                        is_user: 1
                    });
                } else if (wx.getStorageSync('mobile') == "") {
                    this.setData({
                        is_user_show: false,
                        is_user: 2
                    });
                }
            },
            // 登录遮罩层
            close(e) {
                this.setData({
                    is_user_show: e.detail,
                    is_user: 0
                });
            },
            // 登陆后更新
            renew(e) {
                this.setData({
                    showUser: true,
                });
                wx.setStorageSync('userInfo', e.detail)
                wx.setStorageSync('mobile', e.detail.mobile)
            },
    buy() {
        const that = this
        if (that.data.sku == "" || that.data.sku == 0) {
            that.setData({
                is_product: true
            });
        } else {
            if (wx.getStorageSync('mobile') == "") {
                that.login()
                return
            }
            app.http.post(app.api.IntegralMakeOrder, {
                store_id: this.data.store_id,
                address_id: wx.getStorageSync('address').id,
                goods_id: this.data.detail.id,
                spec: this.data.ids,
                sku_product: this.data.sku_product,
                num: this.data.num
            }, (res) => {
                wx.navigateTo({
                    url: '/pages/integral/exchange/index?order_on=' + res.data.order_on,
                })
            })
        }
    },
    getAddressList() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            return
        }
        wx.setStorageSync('address', {
            id: 0
        })
        wx.setStorageSync('add', 0)
        app.http.get(app.api.addressList, {}, (res) => {
            if (res.data.length > 0) {
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].is_default === 1) {
                        wx.setStorageSync('address', res.data[i])
                        break
                    }
                }
            }

        })
    },
    close1(e) {
        console.log(11, e)
        this.setData({
            is_product: e.detail.is_product,
            isChoose: true,
            num: e.detail.num,
            sku: e.detail.sku,
            ids: e.detail.ids,
            sku_product: e.detail.sku_product
        });
    },
    open(e) {
        console.log(e)
        this.setData({
            is_product: true
        });
    },

})